package afi.ch1;

/**
 * @author Pavel Belevich
 *         Date: 6/21/12
 *         Time: 9:50 PM
 */
public class Problem2 {

    static int mySearch(int[] A, int K) {
        
        int start = 0;
        int end = A.length - 1;
        int m;
        while (start <= end) {
            m = start + (end - start) / 2;
            if (K > A[m]) {
                start = m + 1;
            } else if (K < A[m]) {
                end = m - 1;
            } else {
                return m;
            }
        }
        
        return -1;
    }
    
    public static void main(String[] args) {
        int[] A = {1,1,1,3,3,3,6,6,6,6,8,8,10,10,10,15,17,17,19};
        System.out.println(mySearch(A, 0));
        System.out.println(mySearch(A, 1));
        System.out.println(mySearch(A, 6));
        System.out.println(mySearch(A, 8));
        System.out.println(mySearch(A, 15));
        System.out.println(mySearch(A, 19));
        System.out.println(mySearch(A, 20));
    }
    
}
